package com.ruoyi.web.controller.business;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BCase;
import com.ruoyi.system.domain.BTask;
import com.ruoyi.system.domain.BTaskUser;
import com.ruoyi.system.domain.BUserScore;
import com.ruoyi.system.domain.request.CaseRequest;
import com.ruoyi.system.domain.vo.BCaseVo;
import com.ruoyi.system.service.BCaseService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * 案例管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/case")
public class CaseController extends BaseController {

    @Autowired
    BCaseService caseService;

    /**
     * 获取案例列表
     */
    @PreAuthorize("@ss.hasPermi('dict:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(CaseRequest request)
    {
        startPage();
        List<BCaseVo> list = caseService.queryList(request);
        return getDataTable(list);
    }

    /**
     * 根据案例Id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('dict:case:query')")
    @GetMapping(value = "/{caseId}")
    public AjaxResult getInfo(@PathVariable Long caseId) {
        BCaseVo caseVo = caseService.selectCaseById(caseId);
        return success(caseVo);
    }

    /**
     * 创建案例
     */
    @PreAuthorize("@ss.hasPermi('dict:case:create')")
    @Log(title = "创建案例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult create(@RequestBody BCaseVo bCaseVo){
        BCase bCase = new BCase();
        BeanUtils.copyProperties(bCaseVo, bCase);
        bCase.setTransfer(bCaseVo.getChange());
        bCase.setCreateBy(SecurityUtils.getUsername());
        bCase.setCreateTime(new Date());
        caseService.save(bCase);
        return AjaxResult.success(MapUtil.builder().put("id",bCase.getId()).build());
    }

    /**
     * 修改案例
     */
    @PreAuthorize("@ss.hasPermi('dict:case:edit')")
    @Log(title = "修改案例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BCaseVo bCaseVo){
        BCase bCase = new BCase();
        BeanUtils.copyProperties(bCaseVo, bCase);
        bCase.setTransfer(bCaseVo.getChange());
        bCase.setUpdateBy(SecurityUtils.getUsername());
        bCase.setUpdateTime(new Date());
        return toAjax(caseService.updateById(bCase));
    }

    /**
     * 删除案例
     */
    @PreAuthorize("@ss.hasPermi('dict:case:remove')")
    @Log(title = "删除案例", businessType = BusinessType.DELETE)
    @DeleteMapping("/{caseIds}")
    public AjaxResult remove(@PathVariable Long[] caseIds)
    {
        return toAjax(caseService.deleteCaseByIds(caseIds));
    }
}
